# coding=utf-8

import requests

import logging

logger = logging.getLogger('log')


class RequestsLibrary(object):
    """
    封装requests库
    """

    def __init__(self, verify=True):
        self.session = requests.Session()
        self.session.verify = verify

    def user_request(self, method, url, params=None, headers=None, files=None, allow_redirects=True):
        try:
            logger.debug("==========================请求开始==========================")
            if params:
                logger.debug("请求参数： {}".format(params))
            if method.upper() == 'GET':
                response = self.session.get(url=url, params=params, headers=headers, files=files,
                                            allow_redirects=allow_redirects)
                logger.info('响应：{}'.format(response.text))
                return response
            elif method.upper() == 'POST':
                response = self.session.post(url=url, data=params, headers=headers, files=files)
                logger.info('响应：{}'.format(response.text))
                return response
        except Exception as e:
            logger.error("请求异常：{}".format(e))
            self.session.close()

    def user_test_func(self, a, b):
        """
        func
        :param a:
        :param b:
        :return:
        """
        return a, b


if __name__ == '__main__':
    r = RequestsLibrary()
    res = r.user_request(method='get', url="http://www.baidu.com")
    print(res)
